#include"Acceptor.h"
Acceptor::Acceptor(const string&addr,unsigned short&port)
:_addr(addr,port)
,_socket()
{}
Acceptor::~Acceptor(){}

void Acceptor::ready(){
    setRuaddr();
    setRuport();
    bind();
    listen();

}
int Acceptor::accept(){
    //此时已经完成了三次握手
    int netfd = ::accept(_socket.fd(),nullptr,nullptr);
    if(netfd == -1){
        perror("netfd is fail");
        return -1;
    }
    return netfd;
}

int Acceptor::fd(){
    return _socket.fd();
} 

void Acceptor::bind(){
    int ret = ::bind(_socket.fd(),_addr.getsockaddr(),sizeof(struct sockaddr));
    if(ret < 0){
        perror("bind is fail");
    }
}
void Acceptor::listen(){
    ::listen(_socket.fd(),100);
}
void Acceptor::setRuaddr(){
    int flag = 1;
    flag = setsockopt(_socket.fd(),SOL_SOCKET,SO_REUSEADDR,&flag,sizeof(flag));
    if(flag == -1){
        perror("setsokopor addr");
        return;
    }
}
void Acceptor::setRuport(){
    int flag = 1;
    flag = setsockopt(_socket.fd(),SOL_SOCKET,SO_REUSEPORT,&flag,sizeof(flag));
    if(flag == -1){
        perror("setsokopor port");
        return;
    }
}